---
page_title: "cloudflare_worker_script Resource - Cloudflare"
subcategory: ""
description: |-
  Provides a Cloudflare worker script resource. In order for a script to be active, you'll also need to setup a cloudflare_worker_route.
---

# cloudflare_worker_script (Resource)

Provides a Cloudflare worker script resource. In order for a script to be active, you'll also need to setup a `cloudflare_worker_route`.

## Example Usage

```terraform
resource "cloudflare_workers_kv_namespace" "my_namespace" {
  account_id = "f037e56e89293a057740de681ac9abbe"
  title      = "example"
}

# Sets the script with the name "script_1"
resource "cloudflare_worker_script" "my_script" {
  account_id = "f037e56e89293a057740de681ac9abbe"
  name       = "script_1"
  content    = file("script.js")

  kv_namespace_binding {
    name         = "MY_EXAMPLE_KV_NAMESPACE"
    namespace_id = cloudflare_workers_kv_namespace.my_namespace.id
  }

  plain_text_binding {
    name = "MY_EXAMPLE_PLAIN_TEXT"
    text = "foobar"
  }

  secret_text_binding {
    name = "MY_EXAMPLE_SECRET_TEXT"
    text = var.secret_foo_value
  }

  webassembly_binding {
    name   = "MY_EXAMPLE_WASM"
    module = filebase64("example.wasm")
  }

  service_binding {
    name        = "MY_SERVICE_BINDING"
    service     = "MY_SERVICE"
    environment = "production"
  }

  r2_bucket_binding {
    name        = "MY_BUCKET"
    bucket_name = "MY_BUCKET_NAME"
  }

  analytics_engine_binding {
    name    = "MY_DATASET"
    dataset = "dataset1"
  }
}
```
<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `account_id` (String) The account identifier to target for the resource.
- `content` (String) The script content.
- `name` (String) The name for the script. **Modifying this attribute will force creation of a new resource.**

### Optional

- `analytics_engine_binding` (Block Set) (see [below for nested schema](#nestedblock--analytics_engine_binding))
- `compatibility_date` (String) The date to use for the compatibility flag.
- `compatibility_flags` (Set of String) Compatibility flags used for Worker Scripts.
- `d1_database_binding` (Block Set) (see [below for nested schema](#nestedblock--d1_database_binding))
- `kv_namespace_binding` (Block Set) (see [below for nested schema](#nestedblock--kv_namespace_binding))
- `logpush` (Boolean) Enabling allows Worker events to be sent to a defined Logpush destination.
- `module` (Boolean) Whether to upload Worker as a module.
- `placement` (Block Set) (see [below for nested schema](#nestedblock--placement))
- `plain_text_binding` (Block Set) (see [below for nested schema](#nestedblock--plain_text_binding))
- `queue_binding` (Block Set) (see [below for nested schema](#nestedblock--queue_binding))
- `r2_bucket_binding` (Block Set) (see [below for nested schema](#nestedblock--r2_bucket_binding))
- `secret_text_binding` (Block Set) (see [below for nested schema](#nestedblock--secret_text_binding))
- `service_binding` (Block Set) (see [below for nested schema](#nestedblock--service_binding))
- `webassembly_binding` (Block Set) (see [below for nested schema](#nestedblock--webassembly_binding))

### Read-Only

- `id` (String) The ID of this resource.

<a id="nestedblock--analytics_engine_binding"></a>
### Nested Schema for `analytics_engine_binding`

Required:

- `dataset` (String) The name of the Analytics Engine dataset to write to.
- `name` (String) The global variable for the binding in your Worker code.


<a id="nestedblock--d1_database_binding"></a>
### Nested Schema for `d1_database_binding`

Required:

- `database_id` (String) Database ID of D1 database to use.
- `name` (String) The global variable for the binding in your Worker code.


<a id="nestedblock--kv_namespace_binding"></a>
### Nested Schema for `kv_namespace_binding`

Required:

- `name` (String) The global variable for the binding in your Worker code.
- `namespace_id` (String) ID of the KV namespace you want to use.


<a id="nestedblock--placement"></a>
### Nested Schema for `placement`

Required:

- `mode` (String) The placement mode for the Worker. Available values: `smart`.


<a id="nestedblock--plain_text_binding"></a>
### Nested Schema for `plain_text_binding`

Required:

- `name` (String) The global variable for the binding in your Worker code.
- `text` (String) The plain text you want to store.


<a id="nestedblock--queue_binding"></a>
### Nested Schema for `queue_binding`

Required:

- `binding` (String) The name of the global variable for the binding in your Worker code.
- `queue` (String) Name of the queue you want to use.


<a id="nestedblock--r2_bucket_binding"></a>
### Nested Schema for `r2_bucket_binding`

Required:

- `bucket_name` (String) The name of the Bucket to bind to.
- `name` (String) The global variable for the binding in your Worker code.


<a id="nestedblock--secret_text_binding"></a>
### Nested Schema for `secret_text_binding`

Required:

- `name` (String) The global variable for the binding in your Worker code.
- `text` (String, Sensitive) The secret text you want to store.


<a id="nestedblock--service_binding"></a>
### Nested Schema for `service_binding`

Required:

- `name` (String) The global variable for the binding in your Worker code.
- `service` (String) The name of the Worker to bind to.

Optional:

- `environment` (String) The name of the Worker environment to bind to.


<a id="nestedblock--webassembly_binding"></a>
### Nested Schema for `webassembly_binding`

Required:

- `module` (String) The base64 encoded wasm module you want to store.
- `name` (String) The global variable for the binding in your Worker code.

## Import

Import is supported using the following syntax:

```shell
$ terraform import cloudflare_worker_script.example <account_id>/<script_name>
```
